<template>
  <div>
    <!-- 顶部的面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/patient' }">患者信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>基本信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider content-position="left"></el-divider>

    <!-- 操作区域 -->
    <div style="margin: 0 0 20px 0;">
      <el-button type="primary" size="small" @click="$router.push('/admin/patient/add-new')">添加患者</el-button>
    </div>

    <!-- 表格 -->
    <el-divider content-position="left">患者列表</el-divider>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" align="center" width="50"></el-table-column>
      <el-table-column prop="patientId" label="住院号" width="80" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="name" label="姓名" width="60" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="age" label="年龄" width="60" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="dept" label="科室" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="bedId" label="床号" width="60" align="center"></el-table-column>
      <el-table-column prop="disease" label="病症" width="150" align="center"></el-table-column>
      <el-table-column prop="doctor" label="医生" width="60" align="center"></el-table-column>
      <el-table-column prop="admissionTime" label="入院时间" width="100" align="center"></el-table-column>
      <el-table-column prop="dischargeTime" label="出院时间" width="100" align="center"></el-table-column>
      <el-table-column label="详情" width="80" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showArticleDetail(scope.row)">查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控件 -->
    <div style="margin: 10px 0; text-align: right;">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :hide-on-single-page="true"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="total">
      </el-pagination>
    </div>

    <!-- 预览文章详情的弹出框 -->
    <el-dialog class="article-preview-dialog" title="查看患者详情" :visible.sync="articlePreviewDialogVisible">
      <el-descriptions :column="1" border label-class-name="description-label">

        <el-descriptions-item label="住院号">{{ articlePreviewData.patientId }}</el-descriptions-item>
        <el-descriptions-item label="姓名">{{ articlePreviewData.name }}</el-descriptions-item>
        <el-descriptions-item label="年龄">{{ articlePreviewData.age }}</el-descriptions-item>
        <el-descriptions-item label="科室">{{ articlePreviewData.dept }}</el-descriptions-item>
        <el-descriptions-item label="病症详情">{{ articlePreviewData.diseaseDetail }}</el-descriptions-item>
       </el-descriptions>
    </el-dialog>

    <!-- 修改数据的表单 -->
    <el-dialog title="编辑患者数据" :visible.sync="editFormVisible">
      <el-form :model="editForm" :rules="editRules" label-width="120px">

        <el-form-item label="住院号" prop="patientId">
          <el-input v-model="editForm.patientId" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="科室" prop="dept">
          <el-input v-model="editForm.dept" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="editForm.age" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="床号" prop="bedId">
          <el-input v-model="editForm.bedId" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="医生" prop="doctor">
          <el-input v-model="editForm.doctor" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="病症" prop="disease">
          <el-input type="textarea" v-model="editForm.disease"
                    :autosize="{ minRows: 3, maxRows: 5}"
                    maxlength="255" show-word-limit style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="入院时间" prop="admissionTime">
          <el-input v-model="editForm.admissionTime" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="出院时间" prop="dischargeTime">
          <el-input v-model="editForm.dischargeTime" style="width: 400px"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: 20,
      total: 0,
      // 预览文章详情相关数据
      articlePreviewDialogVisible: false,
      articlePreviewData: {},
      // 编辑对话框相关数据
      editFormVisible: false,
      editForm: {
        patientId: '',
        disease: '',
        dept:'',
        bedId:'',
        name:'',
        age:'',
        doctor:'',
        admissionTime:'',
        dischargeTime:''
      },
      // 编辑表单规则
      editRules: {

      }
    };
  },
  methods: {
    // 切换分页
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadArticleList();
    },
    // 切换审核状态

    // 切换显示状态

    // 弹出修改对话框
    openEditDialog(tableItem) {
      let url = 'http://localhost:9980/patient/' + tableItem.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadTagList();
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 执行修改
    handleEdit() {
      let url = 'http://localhost:9980/patient/' + this.editForm.id + '/update/info';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
         // .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改患者成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadTagList();
        } else if (jsonResult.state == 40400) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.editFormVisible = false;
              this.loadTagList();
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 弹出删除确认框
    openDeleteConfirm(tableItem) {
      let message = '此操作将永久删除【' + tableItem.name + '】患者，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(tableItem);
      }).catch(() => {
      });
    },
    // 执行删除
    handleDelete(tableItem) {
      let url = 'http://localhost:9980/patient/' + tableItem.id + '/delete';
      console.log('url = ' + url);

      this.axios
         // .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除标签成功！',
            type: 'success'
          });
          this.loadTagList();
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 显示文章详情
    showArticleDetail(patient) {
      let url = 'http://localhost:9980/patient/' + patient.id;
      console.log('url = ' + url);

      this.axios
         // .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.articlePreviewData = jsonResult.data;
          this.articlePreviewDialogVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadArticleList();
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 加载文章列表
    loadArticleList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9980/patient/list?page=' + page;
      console.log('url = ' + url);

      this.axios
         // .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    }
  },
  mounted() {
    this.loadArticleList();
  }
}
</script>

<style>
.article-preview-dialog {
}

.article-preview-dialog .description-label {
  width: 150px;
}

.article-preview-dialog .picture-item {
  width: 160px;
  height: 90px;
  margin-right: 16px;
  margin-bottom: 10px;
  border-radius: 3px;
  float: left;
}
</style>